﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
using WorkDAL;
using WorkDAL.Meterials;
using Lapas.Common;
using Common;
using System.Threading;
using GrossControl;

namespace Material
{
    public partial class MaterialGroup : ListForm2
    {
        public MaterialGroup()
        {
            InitializeComponent();
            //UIFormat.FormatGrid(MainGrid, false, true, false, false);
        }

        protected override DataGridView Grid
        {
            get
            {
                return this.MainGrid;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ToolStripTextBox toolStripText = new ToolStripTextBox("tstxtMaterialGroupId");
            ListFormToolStrip.Items.Insert(4, toolStripText);

            ToolStripButton item = new ToolStripButton("Tìm", Lapas.Common.Helper.GetBitmap("findIcon"));
            ListFormToolStrip.Items.Insert(5, item);
            this.ListFormToolStrip.Items[5].Click += new System.EventHandler(this.GetData);
        }

        private void GetData(object sender, EventArgs e)
        {
            this.GetData(ListFormToolStrip.Items[4].Text.Trim());
        }

        private void GetData(string materialGroupId)
        {
            try
            {
                List<MaterialGroupObject> liMaterialGroup = MaterialGroupDataAccess.Instance().MaterialGroupGetList(materialGroupId);

                bindingSource.DataSource = liMaterialGroup;
                MainGrid.DataSource = bindingSource;
                BindData();
                FormatBoldGrid();
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
                return;
            }
        }

        protected override void BindData()
        {
            base.BindData();
        }

        protected override void FormatBoldGrid()
        {
            FormatDataGrid.Instance().FormatBoldGrid(MainGrid, ColMortherGroupId.Name, string.Empty);
        }

        private void MaterialGroup_Load(object sender, EventArgs e)
        {
            //Thread.Sleep(2000);
            //this.GetData(string.Empty);
        }

        protected override void DisplayEditForm(EditForm.EditFormAction action)
        {
            MaterialGroupEditForm materialGroupEditForm = new MaterialGroupEditForm();
            materialGroupEditForm.FormAction = action;

            if (action == EditForm.EditFormAction.Change)
            {
                materialGroupEditForm.liMaterialGroup = MaterialGroupDataAccess.Instance().MaterialGroupGetById(((MaterialGroupObject)bindingSource.Current).MaterialGroupId);
            }

            materialGroupEditForm.ShowDialog();
            if (materialGroupEditForm.Reload)
            {
                this.GetData(ListFormToolStrip.Items[4].Text.Trim()); 
            }
        }


        protected override void OnDelete(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridViewRow row in MainGrid.SelectedRows)
                {
                    string id = row.Cells[ColMaterialGroupId.Name].Value.ToString();
                    MaterialGroupDataAccess.Instance().DeleteMaterialGroup(id);
                }
                this.GetData(ListFormToolStrip.Items[4].Text.Trim());
                DisplayMessage.ShowNotice("Đã xóa thành công bản ghi");
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }
        }

        protected override void OnGross(object sender, EventArgs e)
        {
            GrossTableForm grossTableForm = new GrossTableForm();
            grossTableForm.GrossKey = "MaterialGroupId";
            grossTableForm.GrossTable = "LiMaterialGroup";
            grossTableForm.ValueOld = ((MaterialGroupObject)bindingSource.Current).MaterialGroupId;
            grossTableForm.ShowDialog();
        }
    }
}
